package com.java.controller.front;

import java.util.Date;
import java.util.List;

import javax.print.DocFlavor.STRING;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Pattern.Flag;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.java.entity.User;
import com.java.mapper.UserMapper;
import com.java.service.UserService;
import com.java.service.impl.UserServiceImpl;
import com.java.utils.md5Utils;

/**
 * @author Kidney 创建时间：2019年1月22日 上午7:08:18 Description:
 *         http://localhost:8080/huahua//user/selectAll.shtml
 */

@Controller
public class LogController {

	@Autowired
	private UserServiceImpl us;

	/**
	 * 用户登录
	 */
	@RequestMapping("login")
	public ModelAndView login(@RequestParam("username") String username, @RequestParam("password") String password,
			@RequestParam(name = "remember-me", required = false) boolean rememberme, HttpServletRequest request,
			HttpServletResponse response, RedirectAttributes redirectAttributes) {
		HttpSession session = request.getSession();
		User user = new User();
		user.setUserName(username);
		// md5
		String p1 = md5Utils.md5(password);
		user.setPassword(p1);

		// Session中存好的验证码数值
		String piccode = (String) request.getSession().getAttribute("piccode");
		// 前端用户输入的验证码值
		String verifycode = request.getParameter("verifycode");

		String newV = verifycode.toUpperCase();

		User result = us.login(user, rememberme, response);
		if (result != null && newV.equals(piccode)) {
			ModelAndView mAndView = null;
			// 登录之前地址
			String callback = (String) session.getAttribute("callback");
			session.removeAttribute("callback"); // 获取之后移除
			// 基本路径
			String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
					+ request.getContextPath();

			if (StringUtils.isNotBlank(callback) && username.equals("admin")) {
				System.out.println("admin");
				String[] urls = callback.split(basePath);
				if (urls.length == 2 && StringUtils.isNotBlank(urls[1])) {
					mAndView = new ModelAndView("redirect:" + urls[1]);
				} else {
					
					mAndView = new ModelAndView(" /WEB-INF/admin/adminHome.jsp");
				}
			} else if (username.equals("admin")) {
				mAndView = new ModelAndView("/WEB-INF/admin/adminHome.jsp");
			}

			if (StringUtils.isNotBlank(callback) && username.equals("admin") == false) {
				String[] urls = callback.split(basePath);
				if (urls.length == 2 && StringUtils.isNotBlank(urls[1])) {
					mAndView = new ModelAndView("redirect:" + urls[1]);
				} else {
					mAndView = new ModelAndView("/pages/front/index.jsp");
				}
			} else if (username.equals("admin") == false) {
				mAndView = new ModelAndView("/pages/front/index.jsp");
			}

			session.setAttribute("user", result); // 登录成功之后加入session中
			redirectAttributes.addFlashAttribute("user", result);

			return mAndView;
		} else {
			

			ModelAndView mav = new ModelAndView();
			mav.setViewName("/pages/front/login.jsp");
			mav.getModel().put("status", "验证码或密码错误");
		return mav;
		}
	}

	/**
	 * 用户登出
	 */
	@RequestMapping("logout")
	public ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mAndView = new ModelAndView("redirect:/pages/front/login.jsp");
		us.logout(request, response);
		return mAndView;
	}

}
